/*
 * Player.cpp
 *
 *  Created on: 10/08/2010
 *      Author: alex
 */

#include <cmath>
#include "Player.h"

using std::vector;
using std::string;
using utils::makeRect;


static const double PI = 3.1415926;


Player::Player(const string& _stopped, const string& _moving,
		const string& _firing, const string& _defending,
		const string& _hitted, unsigned myNumber) :
GameObject(_stopped), status(PLAYER_STOPPED), angle(0.0), speed(0), maxSpeed(0.1),
moving(_moving, 200, &rect), firing(_firing, 200, &rect),
defending(_defending, &rect), hitted(_hitted, &rect), myNumber(myNumber) {
	life = 100;
}


void Player::render(SDL_Surface *screen) {
	if (life <= 0) return;
	rect.x = x;
	rect.y = y;
	switch (status) {
	case PLAYER_STOPPED:
		stopped.render(screen);
		break;
	case PLAYER_MOVING:
		moving.render(screen);
		break;
	case PLAYER_FIRING:
		firing.render(screen);
		break;
	case PLAYER_DEFENDING:
		defending.render(screen);
		break;
	case PLAYER_HITTED:
		hitted.render(screen);
		break;
	}
}


void Player::move(int dt, float acc) {
	speed += acc *dt;
	if (speed > maxSpeed) speed = maxSpeed;
	else if (speed < -maxSpeed) speed = -maxSpeed;
	x += cos(angle*PI/180) * speed * dt;
	y -= sin(angle*PI/180) * speed * dt;
	status = PLAYER_MOVING;
}


void Player::stop(int dt, float acc) {
	if (speed > 0) {
		speed -= acc * dt;
		if (speed <= 0) {
			speed = 0;
			status = PLAYER_STOPPED;
		}
	} else if (speed < 0) {
		speed += acc * dt;
		if (speed > 0) {
			speed = 0;
			status = PLAYER_STOPPED;
		}
	}
	x += cos(angle*PI/180) * speed * dt;
	y -= sin(angle*PI/180) * speed * dt;
}


Player& Player::bounce(unsigned dt, double angle, float speed) {
	double angle0 = this->angle;
	float speed0 = this->speed;
	this->angle = angle;
	this->speed = speed;
	move(dt, 0);
	this->angle = angle0;
	this->speed = speed0;
	return *this;
}


void Player::rotate(int dt, float incAngle) {
	angle += incAngle * dt;
	while (angle > 360) angle -= 360;
	while (angle < 0) angle += 360;
	stopped.rotate(angle);
	moving.rotate(angle);
	firing.rotate(angle);
	defending.rotate(angle);
	hitted.rotate(angle);
}
